package com.recallgo.manage.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.recallgo.manage.entity.ItemCat;
import com.recallgo.manage.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/item/cat")
public class ItemCatController
{
    @Autowired
    private ItemCatService itemCatService;

    /**
     * 查询
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<List<ItemCat>> queryItemCat(@RequestParam(defaultValue = "0", name = "id") Long parentId){
        // 查询所有商品类型父ID为0的商品类型集合
        EntityWrapper<ItemCat> wrapper = new EntityWrapper<>();
        wrapper.where("parent_id = {0}", parentId);
        List<ItemCat> itemCats = itemCatService.selectList(wrapper);
        if(CollectionUtils.isEmpty(itemCats)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.status(HttpStatus.OK).body(itemCats);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResponseEntity<List<ItemCat>> queryItemCatName(@PathVariable("id") Long id){
        // 查询所有商品类型父ID为0的商品类型集合
        EntityWrapper<ItemCat> wrapper = new EntityWrapper<>();
        wrapper.where("id = {0}", id);
        List<ItemCat> itemCats = itemCatService.selectList(wrapper);
        if(CollectionUtils.isEmpty(itemCats)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.status(HttpStatus.OK).body(itemCats);
    }
}
